<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <!-- 新增角色按钮 -->
        <el-row style="height: 60px">
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            @click="addFn"
          >新增角色</el-button>
        </el-row>

        <!-- 表格 -->
        <el-table :data="list">
          <el-table-column type="index" label="序号" width="120" />
          <el-table-column prop="name" label="角色名称" width="240" />
          <el-table-column prop="description" label="描述" />
          <el-table-column align="center" label="操作" width="240">
            <template slot-scope="scope">
              <el-button size="small" type="success">分配权限</el-button>
              <el-button size="small" type="primary" @click="btnEdit(scope.row.id)">编辑</el-button>
              <el-button size="small" type="danger" @click="deleteRole(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页组件 -->
        <el-row
          type="flex"
          justify="center"
          align="middle"
          style="height: 60px"
        >
          <!-- 分页组件 -->
          <el-pagination
            :current-page="params.page"
            :page-size="params.pagesize"
            :total="params.total"
            layout="prev,pager,next"
            @current-change="changePage"
          />
        </el-row>

        <!-- 新增弹框 -->
        <el-dialog :title="roleForm.id?'编辑角色':'新增角色'" :visible="showDialog" @close="btnClose">
          <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
            <el-form-item label="角色名称" prop="name">
              <el-input v-model="roleForm.name" />
            </el-form-item>
            <el-form-item label="角色描述">
              <el-input v-model="roleForm.description" />
            </el-form-item>
          </el-form>

          <!-- 底部 -->
          <el-row slot="footer" type="flex" justify="center">
            <el-col :span="6">
              <el-button size="small" @click="btnClose">取消</el-button>
              <el-button size="small" type="primary" @click="btnOK">确定</el-button>
            </el-col>
          </el-row>

        </el-dialog>
      </el-card>
    </div>
  </div>
</template>
<script>
import { getRoleList, deleteRole, addRole, getRoleDetail, updateRole } from '@/api/setting'
export default {
  data() {
    return {
      showDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      },
      list: [], // 承接数组
      params: {
        // 放置页码及相关数据
        page: 1,
        pagesize: 3,
        total: 0 // 总条数
      }
    }
  },

  mounted() {
    // 进入渲染数据
    this.getRoleList() // 获取角色列表
  },

  methods: {
    // 获取列表
    async getRoleList() {
      const res = await getRoleList(this.params)
      this.list = res.data.rows
      this.params.total = res.data.total
    },

    // 分页
    changePage(page) {
      this.params.page = page
      this.getRoleList()
    },

    // 添加
    addFn() {
      this.showDialog = true
      this.roleForm.id = ''
      this.roleForm.name = ''
      this.roleForm.description = ''
    },

    // 取消关闭添加角色窗口
    btnClose() {
      this.showDialog = false
    },

    // 确认 添加/修改 角色
    btnOK() {
      this.$refs.roleForm.validate(async validata => {
        if (validata) {
          if (this.roleForm.id.length > 0) {
            await updateRole(this.roleForm)
            this.$message.success('修改成功')
          } else {
            const res = await addRole(this.roleForm)
            console.log(res)
            this.$message.success('添加成功')
          }
          this.getRoleList()
          this.showDialog = false
        }
      })
    },

    // 删除角色
    async deleteRole(id) {
      this.$confirm('删吗,你确定??', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 调用删除接口
        await deleteRole(id)
        // 更新列表
        this.getRoleList()
        // 提示用户
        this.$message.success('删除角色成功')
      }).catch()
    },

    // 编辑角色
    async btnEdit(id) {
      // 打开弹窗
      this.showDialog = true

      // 获取编辑的角色信息
      const res = await getRoleDetail(id)

      // 赋值给输入框
      this.roleForm.name = res.data.name
      this.roleForm.description = res.data.description
      this.roleForm.id = res.data.id
    }
  }
}
</script>
